package com.op.teachingliteapp.util;

import java.util.Map;

import com.op.teachingliteapp.common.BaseApp;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SharedPreferencesUtils {

	private static final String FILE_NAME = "teachinglite_prefs";

	public static SharedPreferences getPreference() {
		return BaseApp.getAppContext().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
	}

	public static SharedPreferences.Editor getEditor() {
		return BaseApp.getAppContext().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE).edit();
	}

	@Deprecated
	public static SharedPreferences getInstance(Context context) {
		return context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
	}

	/**
	 * 保存值
	 */
	@Deprecated
	public static void put(Context context, Map<String, Object> list) {
		if (list != null && list.size() > 0) {
			Editor editor = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE).edit();
			for (String key : list.keySet()) {
				Object value = list.get(key);
				if (value instanceof Integer) {
					editor.putInt(key, (Integer) value);
				}
				if (value instanceof Long) {
					editor.putLong(key, (Long) value);
				}
				if (value instanceof Float) {
					editor.putFloat(key, (Float) value);
				}
				if (value instanceof String) {
					editor.putString(key, (String) value);
				}
				if (value instanceof Boolean) {
					editor.putBoolean(key, (Boolean) value);
				}
			}
			editor.commit();
		}
	}

	/**
	 * 获取key对应的值
	 */
	@Deprecated
	public static String get(String key, Context context) {
		String value = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE).getString(key,
				"");
		return value;
	}
}
